/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ManutencaoPedidoView.java
 *
 * Created on 25/06/2011, 21:36:30
 */

package br.com.comprasPublicas.view.cadastro;

import br.com.comprasPublicas.dao.DAOGenerico;
import br.com.comprasPublicas.entity.ItemPedido;
import br.com.comprasPublicas.entity.Pedido;
import br.com.comprasPublicas.util.HibernateUtil;
import br.com.comprasPublicas.view.pesquisa.PesquisaManutencaoPedidoView;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author helsen
 */
public class ManutencaoPedidoView extends javax.swing.JFrame {

    /** Creates new form ManutencaoPedidoView */
    public ManutencaoPedidoView() {
        initComponents();
        btnConsultar.requestFocus();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        pnlDadosSolicitante = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtCodDepartamento = new javax.swing.JTextField();
        txtDescDepartamento = new javax.swing.JTextField();
        txtCodSecao = new javax.swing.JTextField();
        txtDescSecao = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        txtSolicitante = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblItemPedido = new javax.swing.JTable();
        btnVoltar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        pnlDadosPedido = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        txtCodigo = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        ftxtDataPedido = new javax.swing.JFormattedTextField();
        jLabel6 = new javax.swing.JLabel();
        txtObjetoCompra = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        pnlBotoes = new javax.swing.JPanel();
        btnConsultar = new javax.swing.JButton();
        btnExcluir = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Form"); // NOI18N
        setResizable(false);
        setUndecorated(true);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel1.setName("jPanel1"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(br.com.comprasPublicas.compraspublicas.ComprasPublicasApp.class).getContext().getResourceMap(ManutencaoPedidoView.class);
        pnlDadosSolicitante.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("pnlDadosSolicitante.border.title"))); // NOI18N
        pnlDadosSolicitante.setName("pnlDadosSolicitante"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        txtCodDepartamento.setEditable(false);
        txtCodDepartamento.setName("txtCodDepartamento"); // NOI18N

        txtDescDepartamento.setEditable(false);
        txtDescDepartamento.setName("txtDescDepartamento"); // NOI18N

        txtCodSecao.setEditable(false);
        txtCodSecao.setName("txtCodSecao"); // NOI18N

        txtDescSecao.setEditable(false);
        txtDescSecao.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        txtDescSecao.setName("txtDescSecao"); // NOI18N

        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        txtSolicitante.setEditable(false);
        txtSolicitante.setName("txtSolicitante"); // NOI18N

        javax.swing.GroupLayout pnlDadosSolicitanteLayout = new javax.swing.GroupLayout(pnlDadosSolicitante);
        pnlDadosSolicitante.setLayout(pnlDadosSolicitanteLayout);
        pnlDadosSolicitanteLayout.setHorizontalGroup(
            pnlDadosSolicitanteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlDadosSolicitanteLayout.createSequentialGroup()
                .addGroup(pnlDadosSolicitanteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlDadosSolicitanteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, pnlDadosSolicitanteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, pnlDadosSolicitanteLayout.createSequentialGroup()
                                .addComponent(jLabel7)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtSolicitante))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, pnlDadosSolicitanteLayout.createSequentialGroup()
                                .addGroup(pnlDadosSolicitanteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(txtCodSecao, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtCodDepartamento, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(pnlDadosSolicitanteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(txtDescSecao)
                                    .addComponent(txtDescDepartamento, javax.swing.GroupLayout.DEFAULT_SIZE, 334, Short.MAX_VALUE))))))
                .addContainerGap())
        );
        pnlDadosSolicitanteLayout.setVerticalGroup(
            pnlDadosSolicitanteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlDadosSolicitanteLayout.createSequentialGroup()
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlDadosSolicitanteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtCodDepartamento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtDescDepartamento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlDadosSolicitanteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtCodSecao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtDescSecao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlDadosSolicitanteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(txtSolicitante, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        jScrollPane2.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jScrollPane2.border.title"))); // NOI18N
        jScrollPane2.setName("jScrollPane2"); // NOI18N

        tblItemPedido.setAutoCreateRowSorter(true);
        tblItemPedido.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "item", "Cod.", "Descrição", "Unid.", "Qtde."
            }
        ));
        tblItemPedido.setName("tblItemPedido"); // NOI18N
        tblItemPedido.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        tblItemPedido.getTableHeader().setResizingAllowed(false);
        tblItemPedido.getTableHeader().setReorderingAllowed(false);
        jScrollPane2.setViewportView(tblItemPedido);

        btnVoltar.setFont(resourceMap.getFont("btnVoltar.font")); // NOI18N
        btnVoltar.setMnemonic('v');
        btnVoltar.setText(resourceMap.getString("btnVoltar.text")); // NOI18N
        btnVoltar.setName("btnVoltar"); // NOI18N
        btnVoltar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVoltarActionPerformed(evt);
            }
        });

        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        pnlDadosPedido.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("pnlDadosPedido.border.title"))); // NOI18N
        pnlDadosPedido.setName("pnlDadosPedido"); // NOI18N

        jLabel2.setName("jLabel2"); // NOI18N

        txtCodigo.setEditable(false);
        txtCodigo.setName("txtCodigo"); // NOI18N

        jLabel3.setName("jLabel3"); // NOI18N

        ftxtDataPedido.setEditable(false);
        ftxtDataPedido.setName("ftxtDataPedido"); // NOI18N

        jLabel6.setName("jLabel6"); // NOI18N

        txtObjetoCompra.setEditable(false);
        txtObjetoCompra.setName("txtObjetoCompra"); // NOI18N

        jLabel8.setText(resourceMap.getString("jLabel8.text")); // NOI18N
        jLabel8.setName("jLabel8"); // NOI18N

        jLabel9.setText(resourceMap.getString("jLabel9.text")); // NOI18N
        jLabel9.setName("jLabel9"); // NOI18N

        jLabel10.setText(resourceMap.getString("jLabel10.text")); // NOI18N
        jLabel10.setName("jLabel10"); // NOI18N

        javax.swing.GroupLayout pnlDadosPedidoLayout = new javax.swing.GroupLayout(pnlDadosPedido);
        pnlDadosPedido.setLayout(pnlDadosPedidoLayout);
        pnlDadosPedidoLayout.setHorizontalGroup(
            pnlDadosPedidoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlDadosPedidoLayout.createSequentialGroup()
                .addGroup(pnlDadosPedidoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlDadosPedidoLayout.createSequentialGroup()
                        .addGroup(pnlDadosPedidoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addGroup(pnlDadosPedidoLayout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jLabel9)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(pnlDadosPedidoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3)
                                    .addGroup(pnlDadosPedidoLayout.createSequentialGroup()
                                        .addGap(125, 125, 125)
                                        .addComponent(jLabel8)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(ftxtDataPedido, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                        .addGap(78, 78, 78))
                    .addGroup(pnlDadosPedidoLayout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 470, Short.MAX_VALUE))
                    .addComponent(txtObjetoCompra, javax.swing.GroupLayout.DEFAULT_SIZE, 470, Short.MAX_VALUE)
                    .addGroup(pnlDadosPedidoLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel10)))
                .addContainerGap())
        );
        pnlDadosPedidoLayout.setVerticalGroup(
            pnlDadosPedidoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlDadosPedidoLayout.createSequentialGroup()
                .addGroup(pnlDadosPedidoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel8)
                    .addComponent(jLabel9)
                    .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ftxtDataPedido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlDadosPedidoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(jLabel10))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtObjetoCompra, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(21, Short.MAX_VALUE))
        );

        pnlBotoes.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        pnlBotoes.setName("pnlBotoes"); // NOI18N

        btnConsultar.setMnemonic('n');
        btnConsultar.setText(resourceMap.getString("btnConsultar.text")); // NOI18N
        btnConsultar.setMaximumSize(new java.awt.Dimension(69, 28));
        btnConsultar.setName("btnConsultar"); // NOI18N
        btnConsultar.setPreferredSize(new java.awt.Dimension(69, 28));
        btnConsultar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConsultarActionPerformed(evt);
            }
        });

        btnExcluir.setMnemonic('s');
        btnExcluir.setText(resourceMap.getString("btnExcluir.text")); // NOI18N
        btnExcluir.setName("btnExcluir"); // NOI18N
        btnExcluir.setPreferredSize(new java.awt.Dimension(71, 28));
        btnExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExcluirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlBotoesLayout = new javax.swing.GroupLayout(pnlBotoes);
        pnlBotoes.setLayout(pnlBotoesLayout);
        pnlBotoesLayout.setHorizontalGroup(
            pnlBotoesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlBotoesLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlBotoesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnExcluir, javax.swing.GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE)
                    .addComponent(btnConsultar, javax.swing.GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE))
                .addContainerGap())
        );
        pnlBotoesLayout.setVerticalGroup(
            pnlBotoesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlBotoesLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnConsultar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnExcluir, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btnVoltar)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                            .addComponent(pnlDadosPedido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(pnlDadosSolicitante, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(10, 10, 10)
                            .addComponent(pnlBotoes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(25, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(pnlDadosSolicitante, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(pnlDadosPedido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(pnlBotoes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(45, 45, 45)))
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 255, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnVoltar)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(298, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(118, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnConsultarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConsultarActionPerformed
        if (pesquisaPedidoView == null) {
                    PesquisaManutencaoPedidoView frame = new PesquisaManutencaoPedidoView(this, rootPaneCheckingEnabled);
                    frame.setVisible(true);
                }
}//GEN-LAST:event_btnConsultarActionPerformed

    private void btnExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExcluirActionPerformed
        remove_pedido();
}//GEN-LAST:event_btnExcluirActionPerformed

    private void formWindowActivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowActivated
        //Caso já tenha sido clicado em consultar, e o pedido foi selecionado
        if (ped!=null){
            carrega_pedidoBusca();
            ped = null;
        }
        //Ao iniciar a primeira vez a tela
        else{
            carrega_pedidoFinal();
        }

    }//GEN-LAST:event_formWindowActivated

    private void btnVoltarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVoltarActionPerformed
        Object[] options = { "Sim", "Não" };
        int opcao = JOptionPane.showOptionDialog(
                null,
                "Deseja sair da tela de Manutenção de Pedidos?",
                "Saída de Tela",
                JOptionPane.DEFAULT_OPTION,
                JOptionPane.WARNING_MESSAGE,
                null,
                options,
                options[0]);

        if (opcao == 0) {
            this.dispose();
        }
}//GEN-LAST:event_btnVoltarActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ManutencaoPedidoView().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnConsultar;
    private javax.swing.JButton btnExcluir;
    private javax.swing.JButton btnVoltar;
    private javax.swing.JFormattedTextField ftxtDataPedido;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JPanel pnlBotoes;
    private javax.swing.JPanel pnlDadosPedido;
    private javax.swing.JPanel pnlDadosSolicitante;
    private javax.swing.JTable tblItemPedido;
    private javax.swing.JTextField txtCodDepartamento;
    private javax.swing.JTextField txtCodSecao;
    private javax.swing.JTextField txtCodigo;
    private javax.swing.JTextField txtDescDepartamento;
    private javax.swing.JTextField txtDescSecao;
    private javax.swing.JTextField txtObjetoCompra;
    private javax.swing.JTextField txtSolicitante;
    // End of variables declaration//GEN-END:variables



    private PesquisaManutencaoPedidoView pesquisaPedidoView;
    public static String ped =null;
    private Pedido pedido;
    private int pedidoBusca;
    private ItemPedido itemPedido;

    private void carrega_pedidoBusca() {
        pedido = new Pedido();

        //Listando Pedido
        List<Pedido> lista = new ArrayList ();
        lista = lista_pedido();
        int tamanho_lista = lista.size();

        //Instanciando Pedido selecionado
        for (int i=0; i<tamanho_lista; i++)
        {
            pedido = lista.get(i);
        }

        preenche_objetos();
    }

    private List lista_pedido() {
        Session sessao = HibernateUtil.getSessao();
        Transaction transacao = sessao.beginTransaction();

        //Utilizando criteria para instanciar Pedido.class
        Criteria crit = sessao.createCriteria(Pedido.class);
        pedidoBusca = Integer.parseInt(ped);
        crit.add(Restrictions.eq("id_pedido", pedidoBusca));
        List lista = crit.list();

        transacao.commit();
        sessao.close();

        return lista;
    }

    private void carrega_itens() {
        itemPedido = new ItemPedido();

        //Selecionando itens do pedido
        List<ItemPedido> lista = new ArrayList();
        lista = lista_itens();
        int tam_lista = lista.size();

        DefaultTableModel modelo_tbl = (DefaultTableModel) tblItemPedido.getModel();
        modelo_tbl.setNumRows(0);

        //Instanciando itens do pedido (ItemPedido) e preenchendo-os na tabela
        for (int i=0;i<tam_lista;i++){
            itemPedido = lista.get(i);
            //preenchendo a tabela
            modelo_tbl.addRow(new Object[]{itemPedido.getId_itemPedido(),
                                            itemPedido.getProduto().getId_produto(),
                                            itemPedido.getProduto().getNome(),
                                            itemPedido.getUnidade(),
                                            itemPedido.getQuantidade()
                                            });
        }


    }

    private List<ItemPedido> lista_itens() {
        Session sessao = HibernateUtil.getSessao();
        Transaction transacao = sessao.beginTransaction();

        //Utilizando criteria para instanciar Pedido.class
        Criteria crit = sessao.createCriteria(ItemPedido.class);
        crit.add(Restrictions.eq("pedido", pedido));
        List lista = crit.list();

        transacao.commit();
        sessao.close();

        return lista;
    }

    private void preenche_objetos() {
        String formato = "dd/MM/yyyy";
        SimpleDateFormat formatter = new SimpleDateFormat(formato);
        String dataEmissao = formatter.format(pedido.getData_pedido());
        ftxtDataPedido.setText(dataEmissao);

        txtCodigo.setText(String.valueOf(pedido.getId_pedido()));
        txtObjetoCompra.setText(pedido.getObjeto_compra());
        txtCodDepartamento.setText(String.valueOf(pedido.getSecao().getDepartamento().getId_departamento()));
        txtDescDepartamento.setText(pedido.getSecao().getDepartamento().getDepartamento());
        txtCodSecao.setText(String.valueOf(pedido.getSecao().getId_secao()));
        txtDescSecao.setText(pedido.getSecao().getSecao());
        txtSolicitante.setText(pedido.getSolicitante());

        carrega_itens();
    }

    private void carrega_pedidoFinal() {
        pedido = new Pedido();

        //Listando o ultimo pedido
        List<Pedido> lista = new ArrayList ();
        lista = lista_ultimoPedido();
        int tamanho_lista = lista.size();

        //Instanciando Pedido selecionado
        for (int i=0; i<tamanho_lista; i++)
        {
            pedido = lista.get(i);
        }

        preenche_objetos();
    }

    private List<Pedido> lista_ultimoPedido() {
        DAOGenerico obj_dao_generico = new DAOGenerico(pedido);
        List<Pedido> lista = new ArrayList();
        lista = obj_dao_generico.executa_query("select_max_id_pedido");

        return lista;
    }

    private void remove_pedido() {
        Object[] options = { "Sim", "Não" };
        int opcao = JOptionPane.showOptionDialog(
                null,
                "Confirma exclusão do pedido?",
                "Saída de Tela",
                JOptionPane.DEFAULT_OPTION,
                JOptionPane.WARNING_MESSAGE,
                null,
                options,
                options[0]);

        if (opcao == 0) {
            DAOGenerico obj_pedido = new DAOGenerico(pedido);

            try{
                obj_pedido.excluir();
            }catch(Exception e){
                JOptionPane.showMessageDialog(null, "Não foi possível excluir o pedido: " +e);
            }
            carrega_pedidoFinal();
        }
    }

}
